import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Tests extends InitAndEndBrowser {

    //登录成功的测试用例
    @Order(1)
    @ParameterizedTest
    @CsvSource({"'http://106.53.170.101:8080/java_blog_system/login.html', '李四', '123', 'http://106.53.170.101:8080/java_blog_system/blog_list.html'"})
    void LogIn(String log_url, String name, String password, String blog_list_url) throws InterruptedException {
        webDriver.get(log_url);
        sleep(3000);

        webDriver.findElement(By.cssSelector("#username")).sendKeys(name);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);

        webDriver.findElement(By.cssSelector("#submit")).click();

        sleep(3000);
        String username = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        if (username.equals(name)) {
            System.out.println("登录测试通过");
        } else {
            System.out.println("登录测试不通过");
        }

//        String cur_url = webDriver.getCurrentUrl();
//        if (cur_url.equals(blog_list_url)) {
//            System.out.println("测试通过");
//        } else {
//            System.out.println("测试不通过");
//        }
    }

    @Order(2)
    @Test
    void PublishBlog() throws InterruptedException {
        // 打开博客列表页
        webDriver.get("http://106.53.170.101:8080/java_blog_system/blog_list.html");
        // 点击写博客按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        sleep(3000);
        // document.querySelector("#title").value="测试"
        // 输入标题（通过Selenium执行JS脚本完成）
        ((JavascriptExecutor)webDriver).executeScript("document.querySelector(\"#title-input\").value=\"测试\"");
        sleep(2000);
        // 点击发布博客按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        // 校验
        // 1、找到发布成功的博客标题
        WebElement title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title"));
        sleep(2000);
        // 2、校验标题对应的元素是否为空，如果不为空，测试通过，如果为空，测试不通过
        Assertions.assertNotNull(title);
    }

    @Order(3)
    @Test
    void DeleteBlog() throws InterruptedException {
        webDriver.get("http://106.53.170.101:8080/java_blog_system/blog_list.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > a")).click();
        sleep(2000);
        webDriver.findElement(By.cssSelector("#delete_button")).click();
        sleep(2000);
        String url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://106.53.170.101:8080/java_blog_system/blog_list.html", url);
    }

    @Order(4)
    @Test
    void LogOut() throws InterruptedException {
        webDriver.get("http://106.53.170.101:8080/java_blog_system/blog_list.html");
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        sleep(2000);
        String url = webDriver.getCurrentUrl();
        sleep(3000);
        String username = webDriver.findElement(By.cssSelector("body > div.login-container > div > form > div:nth-child(1) > span")).getText();
        Assertions.assertEquals("http://106.53.170.101:8080/java_blog_system/login.html", url);
        Assertions.assertEquals("用户名", username);
    }

    //登录失败的测试用例
    @Order(5)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginError.csv")
    void LoginError(String log_url, String username, String password, String wrong_text) throws InterruptedException {
        webDriver.get(log_url);
        sleep(3000);

        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);

        webDriver.findElement(By.cssSelector("#submit")).click();

        sleep(1500);
        String wrongInformation = webDriver.findElement(By.cssSelector("body")).getText();

        try {
            Assertions.assertEquals(wrong_text, wrongInformation);
            System.out.println("登录失败测试成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
